﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Collections.Specialized;

namespace Titler
{
	public class ControlMethods
	{
        /// <summary>
        /// Control Add Item function
        /// </summary>
        /// <param name="sender"></param>
        public static void Control_Add_Item(object sender)
        {
            var ThisControl = (System.Windows.Controls.ComboBox)sender;

            if (!string.IsNullOrEmpty(ThisControl.Text))
            {
                //IF WEIGHT, TRIM TO 2 DECIMAL PLACES
                if (ThisControl.Name == "Weight")
                    ThisControl.Text = Math.Round(decimal.Parse(ThisControl.Text), 2).ToString("0.00");

                //IF YIELD, TRIM TO 0 DECIMAL PLACES
                if (ThisControl.Name == "Yield")
                    ThisControl.Text = Math.Round(decimal.Parse(ThisControl.Text), 0).ToString("0");
                
                //CONVERT DIAMETER TO FRACTION
                String AddItem = ThisControl.Name == "Diameter" && !ThisControl.Text.Contains("/") ? GetFraction(decimal.Parse(ThisControl.Text)) : ThisControl.Text;

                //ADD NEW ITEM TO DROPDOWN COMBOBOX
                if (ThisControl.Items.IndexOf(AddItem) == -1)
                {
                    //ASK TO ADD THIS ITEM TO THE COMBOBOX COLLECTION
                    var Result = System.Windows.MessageBox.Show("Would you like to add this to the " + ThisControl.Name + " list?", "CWI Titler",
                        MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);

                    //IF YES MAKE SURE ITS A FRACTION AND ADD TO ITEM LIST AND SETTINGS
                    if (Result == MessageBoxResult.Yes)
                    {
                        ThisControl.Items.Add(AddItem);
                        ((StringCollection)Properties.Settings.Default["List_" + ThisControl.Name]).Add(AddItem);
                        Properties.Settings.Default.Save();
                    }
                }

                //ADD CONVERTED TEXT BACK TO CONTROL
                ThisControl.Text = ThisControl.Name == "Diameter" ? AddItem : ThisControl.Text;
            }
        }




        /// <summary>
        /// Trim Decimal - Round any number to 2 decimals
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string TrimDecimal(decimal num)
        {
            if (!num.ToString().Contains("."))
                return num.ToString();

            return Math.Round(num, 2).ToString();
        }



		/// <summary>
		/// Return the fraction form of the given number, as a string.
		/// </summary>
		/// <param name="num">A number to convert to a fraction</param>
		/// <remarks>
		/// </remarks>
		public static string GetFraction(decimal num)
		{
			if (!num.ToString().Contains("."))
				return num.ToString();
	        
	        int wholeNum;
	        decimal decimalNum;
	        decimal numerator; 
	        decimal denomenator;     
	        
	        wholeNum    = int.Parse(decimal.Truncate(num).ToString());
	        decimalNum  = num - wholeNum;	    
	        numerator   = decimal.Multiply(decimalNum, (decimal)Math.Pow(10, (double)(decimalNum.ToString().Length - 2)));
	        denomenator = (decimal)Math.Pow(10, (double)(decimalNum.ToString().Length - 2));
	        
	        if (numerator == 0)
	        	return wholeNum.ToString();
	        else
	        {
	        	decimal a = numerator;
	        	decimal b = denomenator;
		        decimal t = 0;
		        
	            while (b != 0)
	            {
	            	t = b;
	            	b = decimal.Remainder(a, b);
	                a = t;
	            }
	            if (wholeNum == 0)
	            {
	            	return (decimal.Divide(numerator, a).ToString() + "/" 
	            	        	+ decimal.Divide(denomenator, a).ToString());
	            }
	            else
	            {
	            	return (wholeNum.ToString() + " " + decimal.Divide(numerator, a).ToString() 
	            	        	+ "/" + decimal.Divide(denomenator, a).ToString());
	            }
	        }
		}
	}
}